<?php
	
	/**
	* @author Robbie Vasquez
 	* @author Matt Kennedy
 	* 
	* @param username
	* @param name
	* @param distance
	* @param description
	*/ 
 	
	
class Addroute extends Model
{
	//creating variables
	var $username = '';
	var $name = '';
	var $distance = '';
	var $description = '';
	
	//creating function for addroute
	public function Addroute()
	{
		// Call the model constructor
		parent::Model();
		
		$this->load->database();
	}
	
	//creating function for users to record information
	public function record()
	{
		//creating array for errors
		$errors = array();
	
		//checking if its not empty
		if(isset($_POST) && $_POST != NULL)
		{
			
			if(is_numeric($_POST['distance']))
			{
				//converting miles to kilometers
				if($_POST['unit'] == 'miles')
				{
					$miles = $_POST['distance'];
					$kilometers = $miles * 1.609;
					
					unset($_POST['distance']);
				}
				
				//convertin kilometers to miles
				if($_POST['unit'] == 'kilometers')
				{
					$kilometers = $_POST['distance'];
					$miles = $kilometers / 1.609;
					
					unset($_POST['distance']);
				}
			}
			
			//checking to see if distance entered is a number
			else
				$errors[] = "The distance must be numeric.";
			
			//checking if no errors exist then connect
			if(count($errors) < 1)
			{
				$route = array('username' => $_SESSION['username'], 'name' => $_POST['name'], 'miles' => $miles, 'kilometers' => $kilometers, 'description' => $_POST['description']);
			
				$this->db->insert('routes', $route);
				header("Location: /runningrecords/index.php/listing/listroutes");
				die();
			}
			
			return $errors;
			
		}
	}
}